define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'community/project/schedule/index' + location.search,
                    add_url: 'community/project/schedule/add',
                    edit_url: 'community/project/schedule/edit',
                    del_url: 'community/project/schedule/del',
                    multi_url: 'community/project/schedule/multi',
                    table: 'community_project_schedule',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                rememberOrder: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'community_name', title: __('Community_name'), operate: 'LIKE', sortable: false},
                        {field: 'school_name', title: __('School_name'), operate: 'LIKE', sortable: true},
                        {field: 'project_name', title: __('Project_name'), operate: 'LIKE', sortable: true},
                        {field: 'course_name', title: __('Course_name'), operate: 'LIKE', sortable: true},
                        {field: 'day_of_week', title: __('Day_of_week'), searchList: {
                            '1': __('Monday'),
                            '2': __('Tuesday'),
                            '3': __('Wednesday'),
                            '4': __('Thursday'),
                            '5': __('Friday'),
                            '6': __('Saturday'),
                            '7': __('Sunday')
                        }, formatter: function (value, row, index) {
                            var weekdays = {
                                '1': __('Monday'),
                                '2': __('Tuesday'),
                                '3': __('Wednesday'),
                                '4': __('Thursday'),
                                '5': __('Friday'),
                                '6': __('Saturday'),
                                '7': __('Sunday')
                            };
                            return weekdays[value] || value;
                        }, sortable: true},
                        {field: 'start_time', title: __('Start_time'), operate: 'LIKE', sortable: true},
                        {field: 'end_time', title: __('End_time'), operate: 'LIKE', sortable: true},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status, sortable: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 初始化搜索表单
            var searchForm = $(".form-commonsearch");
            Form.api.bindevent(searchForm);
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 初始化时间选择器
            $('.datetimepicker').datetimepicker({
                format: 'HH:mm:ss',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-history',
                    clear: 'fa fa-trash',
                    close: 'fa fa-remove'
                },
                showClear: true,
                showClose: true
            });
            
            // 表单验证
            Form.api.bindevent($("form[role=form]"), function(data, ret){
                // 成功回调
                Fast.api.close(data);
            }, function(data, ret){
                // 失败回调
                return false;
            });
        },
        edit: function () {
            // 初始化时间选择器
            $('.datetimepicker').datetimepicker({
                format: 'HH:mm:ss',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-history',
                    clear: 'fa fa-trash',
                    close: 'fa fa-remove'
                },
                showClear: true,
                showClose: true
            });
            
            // 表单验证
            Form.api.bindevent($("form[role=form]"), function(data, ret){
                // 成功回调
                Fast.api.close(data);
            }, function(data, ret){
                // 失败回调
                return false;
            });
        }
    };
    return Controller;
});
